package com.ruitu.health_1.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.ruitu.arad.support.widget.dialog.CustomAlertDialog;
import com.ruitu.health_1.R;
import com.ruitu.health_1.ui.adapter.ViewPagerAdapter;

import java.util.ArrayList;


public class ImageViewerActivity extends AppCompatActivity implements View.OnClickListener , ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private TextView tv_num;
    private ImageView iv_del ,iv_back;
    private ArrayList<String> mList;
    private int mPosition;
    private ViewPagerAdapter adapter;

    public static final String IMG_DATA ="image";
    public static final String IMG_POSITION ="position";
    public static final int IMG_REQUEST_CODE =99;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_viewer);
        mList = getIntent().getStringArrayListExtra(IMG_DATA);
        mPosition = getIntent().getIntExtra(IMG_POSITION,0);
        initView();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tv_num = (TextView) findViewById(R.id.tv_num);
        iv_del = (ImageView) findViewById(R.id.iv_del);
        iv_back = (ImageView) findViewById(R.id.iv_back);

        viewPager.addOnPageChangeListener(this);

        adapter = new ViewPagerAdapter(this ,mList);
        viewPager.setAdapter(adapter);
        setPosition();

        iv_back.setOnClickListener(this);
        iv_del.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == iv_back){
            back();
        }
        if (view == iv_del){
            delImg();
        }
    }
    private void back(){
        Intent intent =getIntent();
        intent.putExtra(IMG_DATA,mList);
        setResult(IMG_REQUEST_CODE,intent);
        finish();
    }
    //设置当前位置
    private void setPosition() {
        tv_num.setText(mPosition + 1 + "/" + mList.size());
        viewPager.setCurrentItem(mPosition);
        adapter.notifyDataSetChanged();
    }
    CustomAlertDialog dialog;
    public void delImg(){
          dialog = new CustomAlertDialog(this, "是否删除这张图片?", new CustomAlertDialog.OkOnClickListener() {
             @Override
             public void OkOnClick() {
                 mList.remove(mPosition);
                 setPosition();
                 dialog.dismiss();
             }
         });
        dialog.show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mPosition = position;
        setPosition();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
